<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Model : state
 * di Buat oleh Diar PHP Generator
 * Update List untuk grid karena program generatorku lom sempurna ya hehehehehe */

class modelstate extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getArrayListstate() { /* spertinya perlu lock table */
        $xBuffResul = array();
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state   order by state ASC ";
        $query = $this->db->query($xStr);
        $xBuffResul[0] = '-';
        foreach ($query->result() as $row) {
            $xBuffResul[$row->idx] = $row->state;
        }
        return $xBuffResul;
    }

    function getListstate($xAwal, $xLimit, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where idcountry like '%" . $xSearch . "%'";
        }
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }

    function getListstatebyxountry($xAwal, $xLimit,$xidcountry, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where (state like '%" . $xSearch . "%') and idcountry= '".$xidcountry."'";
        }else{
           $xSearch = "Where  idcountry= '".$xidcountry."'"; 
        }
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }
    
    function getoptionstatebycountry($xidcountry) {
        $xBufResult = '<option value="0">~ State  ~</option>';
        
        
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state WHERE idcountry = '".$xidcountry."' order by state DESC ";
        
        $xQuery = $this->db->query($xStr);
        foreach ($xQuery->result() as $row) {
            
            $xBufResult .= '<option value="' . $row->idx. '" style="">' . $row->state . '</option>';
        }

        return $xBufResult;
    }
    
    function getDetailstate($xidx) {
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    function getLastIndexstate() { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai" .
                " FROM state order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    Function setInsertstate($xidx, $xidcountry, $xstate, $xcode, $xdatein, $xidpegawai) {
        $xStr = " INSERT INTO state( " .
                "idx," .
                "idcountry," .
                "state," .
                "code," .
                "datein," .
                "idpegawai) VALUES('" . $xidx . "','" . $xidcountry . "','" . $xstate . "','" . $xcode . "',now(),'" . $xidpegawai . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    Function setUpdatestate($xidx, $xidcountry, $xstate, $xcode, $xdatein, $xidpegawai) {
        $xStr = " UPDATE state SET " .
                "idx='" . $xidx . "'," .
                "idcountry='" . $xidcountry . "'," .
                "state='" . $xstate . "'," .
                "code='" . $xcode . "'," .
                
                "idpegawai='" . $xidpegawai . "' WHERE idx = '" . $xidx . "'";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    function setDeletestate($xidx) {
        $xStr = " DELETE FROM state WHERE state.idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $this->setInsertLogDeletestate($xidx);
    }

    function setInsertLogDeletestate($xidx) {
        $xidpegawai = $this->session->userdata('idpegawai');
        $xStr = "insert into logdelrecord(idxhapus,nmtable,tgllog,ideksekusi) values($xidx,'state',now(),$xidpegawai)";
        $query = $this->db->query($xStr);
    }

}

?>